<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';


    class dao_bet_history
    {

        //Methods
        public function __construct()
        {

        }

        public function get_bet_history_by_user_id_and_invited_user_id($invited_email, $invite_by_user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_bet_history "
                . "WHERE user_id = " . $invite_by_user_id . " "
                . "AND affiliate_email = '" . $invited_email . "' ";

            $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row;
        }

        public function get_all_win_bet_by_match_id($match_id, $win_choice_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT bh.*, c.name AS choice_name, u.email AS user_email, u.username AS username "
                . "FROM tbl_bet_history bh "
                . "LEFT JOIN tbl_choice c ON bh.choice_id = c.id "
                . "LEFT JOIN tbl_user u ON bh.user_id = u.id "
                . "WHERE bh.match_id = " . $match_id . " "
                . "AND bh.choice_id = " . $win_choice_id . " "
                . "ORDER BY bet_date DESC ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }

        public function count_all_by_match_id($match_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_bet_history "
                . "WHERE match_id = " . $match_id . " ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $count = mysqli_num_rows($result);
            $db->close_connect();

            return $count;
        }

        public function count_all_with_choice_id($match_id, $choice_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_bet_history "
                . "WHERE match_id = " . $match_id . " "
                . "AND choice_id = " . $choice_id;
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $count = mysqli_num_rows($result);
            $db->close_connect();

            return $count;
        }

        public function get_all_by_match_id($match_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT bh.*, c.name AS choice_name, rg.name AS game_name, u.email AS user_email, u.username AS username "
                . "FROM tbl_bet_history bh "
                . "LEFT JOIN tbl_choice c ON bh.choice_id = c.id "
                . "LEFT JOIN tbl_user u ON bh.user_id = u.id "
                . "LEFT JOIN tbl_roulette_game rg ON rg.id = c.game_id "
                . "WHERE bh.match_id = " . $match_id . " "
                . "ORDER BY bet_date DESC ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }

        public function get_all_by_user_id($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT bh.*, c.name AS choice_name, u.email AS user_email "
                . "FROM tbl_bet_history bh "
                . "LEFT JOIN tbl_choice c ON bh.choice_id = c.id "
                . "LEFT JOIN tbl_user u ON bh.user_id = u.id "
                . "WHERE user_id = " . $user_id . " "
                . "ORDER BY bet_date DESC ";

            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }

        public function get_all()
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_bet_history ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }

        public function save($id, $match_id, $bet_amount, $user_id, $choice_id, $affiliate_email)
        {
            $db  = new connection();
            $con = $db->open_connect();

            if ($id == 0) {
                $query = "INSERT INTO tbl_bet_history(match_id, bet_amount, user_id, choice_id, bet_date, affiliate_email) VALUES (
                " . $match_id . "," . $bet_amount . "," . $user_id . "," . $choice_id . ",'" . lib_date::get_now() . "','" . $affiliate_email . "')";
            } else {
                $query = "UPDATE tbl_bet_history SET "
                    . "match_id = " . $match_id . ","
                    . "bet_amount = " . $bet_amount . ","
                    . "user_id = " . $user_id . ","
                    . "choice_id = " . $choice_id . ","
                    . "affiliate_email = '" . $affiliate_email . "' "
                    . "WHERE id = " . $id;
            }

            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function get_by_id($id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_bet_history "
                . "WHERE id = " . $id;
            $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row;
        }

        public function delete($id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "DELETE FROM tbl_bet_history WHERE id = " . $id;
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function get_bet_history_of_user($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_bet_history WHERE user_id = $user_id";
            $result = mysqli_query($con, $query) or die(mysqli_error());
            $list = array();

            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $db->close_connect();

            return $list;
        }

        public function get_win_choice_id_of_match($match_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_match WHERE id = $match_id";
            $result = mysqli_query($con, $query) or die(mysqli_error());
            $row           = mysqli_fetch_array($result);
            $win_choice_id = $row['win_choice_id'];

            $db->close_connect();

            return $win_choice_id;
        }

        public function get_bet_history($bet_history_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_bet_history WHERE id = $bet_history_id";
            $result = mysqli_query($con, $query) or die(mysqli_error());
            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row;
        }

    }
